package controller.managedBeans;

import java.io.Serializable;
import java.util.*;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.*;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.validation.constraints.*;

import org.richfaces.event.FileUploadEvent;
import org.richfaces.event.ItemChangeEvent;
import org.richfaces.model.UploadedFile;

import model.entities.*;
import model.dao.*;



@ManagedBean(name = "ticketBean")
@SessionScoped
public class TicketBackingBean   implements Serializable  {

   private static final long serialVersionUID = 1L;
	
   //inject the needed EJB
   @EJB
   private TicketDao ticketFacade;
   
   @EJB
   private TicketTypeDao typeFacade;
   
   @EJB
   private GroupDao groupFacade;
   
   @EJB
   private PriorityDao priorityFacade; 

   @EJB
   private UserDao userFacade;
   
   
   private Ticket ticket ;
   private List<User> allUsers = new ArrayList<User>();
   private List<User> groupUsers = new ArrayList<User>() ;
   private List<Group> groups ;
   private List<TicketType> types ;
   private List<Priority> priorities ;
   private User currentUser ;
   private boolean ticketCreated ;
   
	@PostConstruct
    public void init() {
		System.out.println("initialization");
		allUsers  = userFacade.findAll();
		groups = groupFacade.findAll();
		types  = typeFacade.findAll();
		priorities = priorityFacade.findAll();
		currentUser = userFacade.findUserWithTickets(new Integer(1));	
	    prepareNewTicket();
    }

	public void listener(FileUploadEvent event) throws Exception
	{
		UploadedFile item = event.getUploadedFile();
		ticket.setAttachedFile(item.getData());
	}
	
	public String clearUploadData()
	{
		ticket.setAttachedFile(null);
		return null ;
	}
	
	public void updateGroupUsers()
	{
		System.out.println("begin group "); 
		 groupUsers.clear();
		 int groupId = ticket.getAssignedGroup().getGroupId().intValue() ;
		 System.out.println("groupId = " + groupId);
		 for (int i = 0; i < allUsers.size(); i++) 
		 {
			 System.out.println(" int the loop ");
			 User user = allUsers.get(i) ;
			 System.out.println(" 55555555 ");
			 if(user.getGroup()!=null && user.getGroup().getGroupId().intValue() == groupId)
			 {
				 System.out.println(" 222222222 ");
				 groupUsers.add(user) ;
				 System.out.println(" 33333333 ");
			 }
	     }	
	}

	public void insertTicket()
	{
		try
		{
			if(isValid())
			{
				System.out.println("adding ticket");
				ticketFacade.create(ticket);
				System.out.println("ticket created.");
				setTicketCreated(true);
				prepareNewTicket();
				//setSucceeded(true);
				System.out.println("data prepared");
			}
			
		}
		catch(Exception e )
		{
			e.printStackTrace();
		}
		
	}
	
	// for validation
	public boolean isValid()
	{
		return true ;
	}
	
	
	
	public void cancelTicket()
	{
		prepareNewTicket();
	}

	
	public void prepareNewTicket()
	{	
    	ticket = new Ticket();
		ticket.setIssueDate(new Date());
		ticket.setInitiatorUser(currentUser);
	}
	

	
	
	
	
	public Ticket getTicket() {
		return ticket;
	}


	public void setTicket(Ticket ticket) {
		this.ticket = ticket;
	}






	public User getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	public boolean isTicketCreated() {
		return ticketCreated;
	}

	public void setTicketCreated(boolean ticketCreated) {
		this.ticketCreated = ticketCreated;
	}

	public List<User> getAllUsers() {
		return allUsers;
	}


	public void setAllUsers(List<User> allUsers) {
		this.allUsers = allUsers;
	}


	public List<User> getGroupUsers() {
		return groupUsers;
	}


	public void setGroupUsers(List<User> groupUsers) {
		this.groupUsers = groupUsers;
	}


	public List<Group> getGroups() {
		return groups;
	}


	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}


	public List<TicketType> getTypes() {
		return types;
	}


	public void setTypes(List<TicketType> types) {
		this.types = types;
	}


	public List<Priority> getPriorities() {
		return priorities;
	}


	public void setPriorities(List<Priority> priorities) {
		this.priorities = priorities;
	}
	
	
	
	
}
